using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;

namespace SP2013.MetadataNavigation.Features.SP2013.MetadataNavigation
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("2bcc6022-22d8-4d4b-b887-fc48488fe79d")]
    public class SP2013EventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            using (SPSite sitecollection = (SPSite)properties.Feature.Parent)
            {
                using (SPWeb web = sitecollection.RootWeb)
                {
                    string WebAppRelativePath = sitecollection.ServerRelativeUrl;
                    if (!WebAppRelativePath.EndsWith("/"))
                    {
                        WebAppRelativePath += "/";
                    }

                    foreach (SPWeb site in sitecollection.AllWebs)
                    {
                        site.MasterUrl = WebAppRelativePath + "_catalogs/masterpage/seattleGlobalNav.master";
                        site.CustomMasterUrl = WebAppRelativePath + "_catalogs/masterpage/seattleGlobalNav.master";
                        site.SiteLogoUrl = "";
                        site.Update();
                    }
                }
            }
        }

        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            string WebAppRelativePath = string.Empty;

            using (SPSite sitecollection = (SPSite)properties.Feature.Parent)
            {
                using (SPWeb web = sitecollection.RootWeb)
                {
                    WebAppRelativePath = sitecollection.ServerRelativeUrl;
                    if (!WebAppRelativePath.EndsWith("/"))
                    {
                        WebAppRelativePath += "/";
                    }

                    foreach (SPWeb site in sitecollection.AllWebs)
                    {
                        site.MasterUrl = WebAppRelativePath + "_catalogs/masterpage/seattle.master";
                        site.CustomMasterUrl = WebAppRelativePath + "_catalogs/masterpage/seattle.master";
                        site.SiteLogoUrl = "";
                        site.Update();
                    }
                }
            }

            DeleteMasterPage(properties, WebAppRelativePath);
        }

        private void DeleteMasterPage(SPFeatureReceiverProperties properties, string WebAppRelativePath)
        {
            using (SPSite sitecollection = (SPSite)properties.Feature.Parent)
            {
                using (SPWeb web = sitecollection.RootWeb)
                {
                    // Delete custom master page
                    SPFile file = web.GetFile(WebAppRelativePath + "_catalogs/masterpage/seattleGlobalNav.master");
                    if (file.Exists)
                        file.Delete();
                    file.Update();
                }
            }
        }


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
